

clean_australia <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){
 
  if (yr == 2001) {
    db <- subset(db, sample == "Mail survey") # Dropping internet respondents, can't find codebook to figure out questionaire details.
  }
   
  if (yr == 2007) {
   db <- db  %>%  mutate(b22 = as.character(b22))
    db$b22[which(as.character(db$b22) == "Labor Party (ALP)")] <- "Labor"
    db$b22[which(as.character(db$b22) == "Liberal Party")]     <- "Liberal"
  }
  
  return(db)
}


clean_britain <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){
  
  # Create consistent party naming from two different questions that were randomized across
  if (yr == 1992){
    db$SPECIAL <- NA
    db$SPECIAL[which(as.character(db$v5) == "odd:ask b version")]  <- as.character(db$vb13a[which(as.character(db$v5) == "odd:ask b version")])
    db$SPECIAL[which(as.character(db$v5) == "even:ask a version")] <- as.character(db$va6a[which(as.character(db$v5)  == "even:ask a version")])
    
    db$SPECIAL_lean <- NA
    db$SPECIAL_lean[which(as.character(db$v5) == "odd:ask b version")]  <- as.character(db$vb13b[which(as.character(db$v5) == "odd:ask b version")])
    db$SPECIAL_lean[which(as.character(db$v5) == "even:ask a version")] <- as.character(db$va6b[which(as.character(db$v5)  == "even:ask a version")])
  }
  if (yr == 2005){
    db$SPECIAL <- NA
    db$SPECIAL[which(db$xyver2 == 1)] <- as.character(db$bq9ax[which(db$xyver2 == 1)])
    db$SPECIAL[which(db$xyver2 == 2)] <- as.character(db$bq9ay[which(db$xyver2 == 2)])
    
    db$SPECIAL_lean <- NA
    db$SPECIAL_lean[which(db$xyver2 == 1)] <- as.character(db$bq9cx[which(db$xyver2 == 1)])
    db$SPECIAL_lean[which(db$xyver2 == 2)] <- as.character(db$bq9cy[which(db$xyver2 == 2)])
  }
  
  if (yr == 2015 | yr == 2017) {
    db <- db  %>%  mutate(d03 = as.character(d03))
    db$d03[which(as.character(db$d03) == "Conservatives")]     <- "Conservative"
    db$d03[which(as.character(db$d03) == "Liberal Democrats")] <- "Liberal Democrat"
  }
  
  # Drop NA/"not app" observations for party affiliation
  db <- db[!(db[, as.character(varname_yr$party_var)] == "not app" | is.na(db[, as.character(varname_yr$party_var)])), ] # All years have "None", "DK", "other", "refused" or similar options, but some years have large NAs or "not app". These likely indicate respondents for which the question was not asked.
  
  return(db)
}



clean_canada <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){
  if (yr == 1968){ 
    db <- db  %>%  mutate(var134 = as.character(var134))
    db$var134[which(as.character(db$var134) == "LIB")] <- "LIBERAL" # Make sure leaners and party affiliation codings match
  }
  # Fix parties where exact question was randomized
  if (yr == 2000){
    db$SPECIAL <- NA
    db$SPECIAL[!is.na(db$cpsk1a)] <- as.character(db$cpsk1a[!is.na(db$cpsk1a)])
    db$SPECIAL[!is.na(db$cpsk1b)] <- as.character(db$cpsk1b[!is.na(db$cpsk1b)])
    
    db$SPECIAL_lean <- NA
    db$SPECIAL_lean[!is.na(db$cpsk4a)] <- as.character(db$cpsk4a[!is.na(db$cpsk4a)])
    db$SPECIAL_lean[!is.na(db$cpsk3b)] <- as.character(db$cpsk3b[!is.na(db$cpsk3b)])
  }
  if (yr == 2004){
    db$SPECIAL <- NA
    db$SPECIAL[!is.na(db$cps_q1a)] <- as.character(db$cps_q1a[!is.na(db$cps_q1a)])
    db$SPECIAL[!is.na(db$cps_q1b)] <- as.character(db$cps_q1b[!is.na(db$cps_q1b)])
    
    db <- db  %>%  mutate(cps_q4 = as.character(cps_q4))
    db$cps_q4[which(as.character(db$cps_q4) == "conservative (new party)")] <- "conservative (new conservative party)"
  }
  
  # Drop NA observations for party affiliation for certain years where this likely indicates the question not asked
  if (yr == 1993 | yr == 2008 | yr == 2015) db <- db[!(is.na(db[, as.character(varname_yr$party_var)])), ]  # These are years with large number of NAs which have separate "None", "DK", "Refused" options. So these were respondents that likely were never asked the question. We don't want to drop all NAs across years because the early years don't have separate "DK" or "None" options.
  
  return(db)
}




clean_denmark <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){
  
  if (yr == 1994){
    levels(db$V116) <- c(levels(db$V116), "Enhedslisten")
    db$V116[which(as.character(db$V116) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V118) <- c(levels(db$V118), "Enhedslisten")
    db$V118[which(as.character(db$V118) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V118) <- c(levels(db$V118), "Z. Fremskridtspartiet")
    db$V118[which(as.character(db$V118) == "Z: Fremskridtspartiet")] <- "Z. Fremskridtspartiet"
    
    for (v in c("V146", "V147", "V148", "V149", "V150", "V151", "V152", "V153", "V154")){
      levels(db[, v]) <- c(levels(db[, v]), "10", "Deltog ikke")
      db[, v][which(as.character(db[, v]) == "Skala 10 (Højest)")] <- "10"
      db[, v][which(as.character(db[, v]) == "Deltog ikke (Variablen er kun genereret p\xe5 aggrund af besvar")] <- "Deltog ikke"
    }
  } else if (yr == 1998){
    levels(db$V60) <- c(levels(db$V60), "Enhedslisten")
    db$V60[which(as.character(db$V60) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V62) <- c(levels(db$V62), "Enhedslisten")
    db$V62[which(as.character(db$V62) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V62) <- c(levels(db$V62), "C: Det Konservative Folkeparti")
    db$V62[which(as.character(db$V62) == "C: Det konservative Folkeparti")] <- "C: Det Konservative Folkeparti"
    
    for (v in c("V37", "V38", "V39", "V40", "V41", "V42", "V43", "V44", "V45", "V46", "V47")){
      levels(db[, v]) <- c(levels(db[, v]), "0")
      db[, v][which(as.character(db[, v]) == "Synes meget dårligt om")] <- "0"
    }
  } else if (yr == 2001){
    levels(db$V50) <- c(levels(db$V50), "Enhedslisten")
    db$V50[which(as.character(db$V50) == "Ø. Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V52) <- c(levels(db$V52), "Enhedslisten")
    db$V52[which(as.character(db$V52) == "Ø. Enhedslisten")] <- "Enhedslisten"
    
    for (v in c("V33", "V34", "V35", "V36", "V37", "V38", "V39", "V40")){
      levels(db[, v]) <- c(levels(db[, v]), "0")
      db[, v][which(as.character(db[, v]) == "Synes meget dårligt om partiet")] <- "0"
    }
  } else if (yr == 2005){
    levels(db$V117) <- c(levels(db$V117), "Enhedslisten")
    db$V117[which(as.character(db$V117) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V119) <- c(levels(db$V119), "Enhedslisten")
    db$V119[which(as.character(db$V119) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    for (v in c("V69", "V70", "V71", "V72", "V73", "V74", "V75", "V76", "V77", "V78")){
      levels(db[, v]) <- c(levels(db[, v]), "0")
      db[, v][which(as.character(db[, v]) == "Synes meget dårligt om partiet")] <- "0"
    }
  } else if (yr == 2007){
    levels(db$V118) <- c(levels(db$V118), "Enhedslisten")
    db$V118[which(as.character(db$V118) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V120) <- c(levels(db$V120), "Enhedslisten")
    db$V120[which(as.character(db$V120) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V120) <- c(levels(db$V120), "A: Socialdemokraterne")
    db$V120[which(as.character(db$V120) == "A: Socialdemokratiet")] <- "A: Socialdemokraterne"
    
    for (v in c("V87", "V88", "V89", "V90", "V91", "V92", "V93", "V94", "V95")){
      levels(db[, v]) <- c(levels(db[, v]), "0")
      db[, v][which(as.character(db[, v]) == "0. Synes meget dårligt om partiet")] <- "0"
    }
  } else if (yr == 2011){
    levels(db$V94) <- c(levels(db$V94), "Enhedslisten")
    db$V94[which(as.character(db$V94) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    levels(db$V96) <- c(levels(db$V96), "Enhedslisten")
    db$V96[which(as.character(db$V96) == "Ø: Enhedslisten")] <- "Enhedslisten"
    
    for (v in c("V77", "V78", "V79", "V80", "V81", "V82", "V83", "V84")){
      levels(db[, v]) <- c(levels(db[, v]), "0")
      db[, v][which(as.character(db[, v]) == "0 - Synes meget dårligt om partiet")] <- "0"
    }
  } else if (yr == 2015){
    levels(db$V88) <- c(levels(db$V88), "Enhedslisten", "Alternativet")
    db$V88[which(as.character(db$V88) == "Ø: Enhedslisten")] <- "Enhedslisten"
    db$V88[which(as.character(db$V88) == "Å: Alternativet")] <- "Alternativet"
    
    levels(db$V90) <- c(levels(db$V90), "Enhedslisten", "Alternativet")
    db$V90[which(as.character(db$V90) == "Ø: Enhedslisten")] <- "Enhedslisten"
    db$V90[which(as.character(db$V90) == "Å: Alternativet")] <- "Alternativet"
    
    for (v in c("V67", "V68", "V69", "V70", "V71", "V72", "V73", "V74", "V75")){
      levels(db[, v]) <- c(levels(db[, v]), "0")
      db[, v][which(as.character(db[, v]) == "Synes meget dårligt om partiet")] <- "0"
    }
  }
  
  return(db)
}

clean_france <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){
  if (yr == 1967){
    db[db[, varname_yr$party_var] == 51, varname_yr$party_var] <- 50 # We grouped “50 = UNR (nothing more precise),” of which there were 353 observations, and “51 = UNR - leftist; Gaullists of the left; UDT,” of which there were 4 observations, together.
  }
  
  return(db)
}


clean_japan <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){
  if (yr == 1967){
    db <- db %>% filter(V15 == 1) # Exclude "youth" respondents
  }
  if (yr == 1976){
    db <- db %>% filter(PRE_FILT != "Not interviewed")
  }
  
  return(db)
}


clean_new_zealand <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){

  # Pre-cleaning NA party responses
  # "0" == "No party attachment"
  # "7" == "Another party (please say what)"
  party_var_yr  = as.character(varname_yr$party_var)
  if (yr == 1990){
    db[db[, party_var_yr] %in% c(0, 7), party_var_yr] <- NA
  }
  if (yr == 1996){
    # Drops phone interview respondents for which the party affiliation question lists "Not Applicable - Phone Interview"
    db <- db[db[, party_var_yr] != "Not Applicable - Phone Interview", ]  
  }
  if (yr == 2005) {
    db <- db  %>%  mutate(ypclose = as.character(ypclose))
    db$ypclose[which(as.character(db$ypclose) == "Socred")] <- "Social Credit"
  }
  
  return(db)
}



clean_norway <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){
  
  thermo_vars_pre_clean  <- as.character(thermo_vars_yr[, -c(1:2)])
  party_var_yr           <- as.character(varname_yr$party_var)
  for (k in 1:length(thermo_vars_pre_clean)){
    if (thermo_vars_pre_clean[k] != "XXX"){
      if (yr %in% c(1981, 1985)){
        db[db[, thermo_vars_pre_clean[k]] == 999, thermo_vars_pre_clean[k]] <- NA
      }
    }
  }
  
  # Clean party affiliation variables
  if (yr == 1997){
    db$spm20a1 <- as.character(db$spm20a1)
    db$spm20a1[is.na(db$spm20a1) & db$spm20 == 5] <- "No party"  # Change 'NA' to 'No Party' for individuals that said they were not close to any particular party.
  }
  if (yr == 2001) { # Fixes wrong labelling
    temp <- db$v293
    db$v293[as.character(temp) == "Liberal People's Party (DLF)"] <- "Conservative Party (H)"
    db$v293[as.character(temp) == "Conservative Party (H)"]       <- "Progress Party (FrP)"
    db$v293[as.character(temp) == "Progress Party (FrP)"]         <- "Other parties/lists"
  } 
  if (yr != 2009){
    db <- db[!is.na(db[, party_var_yr]), ] # Drop NAs for years that have separate "None", "DK', "Other", "Refused" and related responses. These are likely respondents that were not asked these questions and are oftenlisted as "invalid" or "sysmiss" in the codebook.
  }
  
  return(db)
}


clean_sweden <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){

  if (yr == 1985) {
    db <- db  %>%  mutate(v192 = as.character(v192))
    db$v192[which(as.character(db$v192) == "centre party (c)  ")] <- " centre party (c)   "
    # Be careful about the white space
  }
  
  if (yr == 1988) {
    db <- db  %>%  mutate(v169 = as.character(v169))
    db$v169[which(as.character(db$v169) == "people's party")] <- "people's party liberals"
  }
  
  #Variables v4, v8, v7, c57, and VU10_V7051 described whether a participant was interviewed
  #before or after an election. If the variable was NA, then the partipant was not interviewed.
  if (yr %in% c(1979, 1982, 1985, 1988)){
    db <- db %>% filter(!is.na(v4))
  } else if(yr == 1991){
    db <- db %>% filter(!is.na(v8))
  } else if(yr %in% c(1994, 1998, 2002)){
    db <- db %>% filter(!is.na(v7))
  } else if(yr == 2006){
    db <- db %>% filter(!is.na(c57))
  } else if(yr == 2010){
    db <- db %>% filter(!is.na(VU10_V7051))
  }
  
  return(db)
}



clean_switzerland <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){
  if (yr == 1975 && alt == TRUE){
    parties_1975     <- parties_yr[, -c(1:2)]
    thermo_vars_1975 <- as.character(thermo_vars_yr[, -c(1:2)])
    for (i in 1:nrow(db)){
      for (k in 1:length(parties_1975)){
        if (thermo_vars_1975[k] %in% sprintf("sypa%s", c(1, 3, 8, 10))){
          db <- db  %>%
            mutate(pid2b = as.character(pid2b))
          parties_1975[k] <- as.character(parties_1975[k])
          if (db[i, "pid2b"] != parties_1975[k] & (is.na(db[i, thermo_vars_1975[k]]) | db[i, thermo_vars_1975[k]] == "dk")) db[i, thermo_vars_1975[k]] <- "no sympathy"
        }
      }
    }
  }
  
  return(db)
}


clean_united_states <- function(db, yr, country, varname_yr, parties_yr, thermo_vars_yr, alt = F){

  if (yr != 2020){
    if (alt) db <- db %>% filter(VCF0017 != "4. All internet (2012: pre and post)")
    
    db <- db  %>%
      mutate(VCF0301 = as.character(VCF0301))
    db$VCF0301[which(as.character(db$VCF0301) == "1. Strong Democrat")]          <- "5. Democrat"
    db$VCF0301[which(as.character(db$VCF0301) == "2. Weak Democrat")]            <- "5. Democrat"
    db$VCF0301[which(as.character(db$VCF0301) == "3. Independent - Democrat")]   <- "5. Democrat"
    db$VCF0301[which(as.character(db$VCF0301) == "5. Independent - Republican")] <- "1. Republican"
    db$VCF0301[which(as.character(db$VCF0301) == "6. Weak Republican")]          <- "1. Republican"
    db$VCF0301[which(as.character(db$VCF0301) == "7. Strong Republican")]        <- "1. Republican"
    
  }
  if (yr == 2020) {
    if (alt) db <- db  %>% filter(V200002 != 3)
    
    # Fix leaner codings to align with party id codings for ANES 2020
    db$V201230[db$V201230 == 2] <- 0 # Neither to 0
    db$V201230[db$V201230 == 1] <- 2 # R to 2
    db$V201230[db$V201230 == 3] <- 1 # D to 1

  }
  
  return(db)
}



